package com.horoscope.astrology.zodiac.signs.free.adapter.quize

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.QuestionBean
import com.horoscope.astrology.zodiac.signs.free.utils.ImageLoader


class QuizImageAdapter(private val mContext: Context) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mDataList = ArrayList<String>()
//    private var loadFinished: Boolean = false
    private var mBean = QuestionBean()

    fun setBean(bean: QuestionBean) {
        mBean = bean
        mDataList.addAll(0, bean.answer)
        notifyDataSetChanged()

    }


    override fun onCreateViewHolder(parent: ViewGroup, p1: Int): RecyclerView.ViewHolder {

        return when (p1) {
            HEAD -> {
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_quiz_head, parent, false)
                return HeadViewHolder(view)
            }
            QUESTION -> {
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_quiz_question2, parent, false)
                QuestionViewHolder(view)
            }
            ANSWER -> {
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_quiz_answer_image, parent, false)
                AnswerViewHolder(view)
            }
            else -> {
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_quiz_answer_image, parent, false)
                AnswerViewHolder(view)
            }
        }

    }

    override fun getItemCount(): Int {
        return if (mDataList.isEmpty()) {
            0
        } else {
            mDataList.size + 1
        }
    }


    private val array = arrayOf(false, false, false, false)
    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, p1: Int) {
        when (holder) {
            is HeadViewHolder -> {

                ImageLoader.loadImage(holder.title, mBean.imageUrl)

            }
            is QuestionViewHolder -> {

                holder.question.text = mBean.text
            }
            is AnswerViewHolder -> {
                if (p1 - 1 < mDataList.size && p1 - 1 >= 0) {

                    ImageLoader.loadImage(holder.answer, mDataList[p1 - 1])
                    if (array[p1 - 1]) {
                        holder.foreground.setBackgroundColor(ContextCompat.getColor(mContext, R.color.purple_7))
                        holder.icon.visibility = View.VISIBLE
                    } else {
                        holder.foreground.setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent))
                        holder.icon.visibility = View.INVISIBLE
                    }
                    holder.itemView.setOnClickListener {
                        array[p1 - 1] = true
                        for (i in array.indices) {
                            if (i != (p1 - 1)) {
                                array[i] = false
                            }
                        }
                        notifyDataSetChanged()
                    }
                }
            }
        }

    }


    override fun getItemViewType(position: Int): Int {
        return when (position) {
            /*0 -> {
                HEAD
            }*/
            0 -> {
                QUESTION
            }
            else -> {
                ANSWER
            }
        }
    }

    companion object {
        const val HEAD = 0x14
        const val QUESTION = 0x15
        const val ANSWER = 0x16
    }


    open inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    inner class HeadViewHolder(itemView: View) : MyViewHolder(itemView) {
        val title = itemView.findViewById<ImageView>(R.id.image_view_head)!!
    }

    inner class QuestionViewHolder(itemView: View) : MyViewHolder(itemView) {
        val question = itemView.findViewById<TextView>(R.id.text_view_question)!!
    }

    inner class AnswerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val answer: ImageView = itemView.findViewById(R.id.image_view_quiz_answer)
        val foreground = itemView.findViewById<View>(R.id.view_foreground)
        val icon = itemView.findViewById<ImageView>(R.id.image_view_select)

    }

}